jQuery(document).ready(function() {    
	App.init(); 
	TableManaged.init();
})

$("body").bind('keyup',function(event) {  
	if( event.keyCode == 13 ) {  
	    document.form.submit();  
	}     
}); 

var $window = $(window), $subnav = $(".page-sidebar-menu"), url;
/* 左边菜单高亮 */
url = window.location.pathname + window.location.search;
url = url.replace(/(\/(p)\/\d+)|(&p=\d+)|(\/(id)\/\d+)|(&id=\d+)|(\/(group)\/\d+)|(&group=\d+)/, "");
$subnav.find("a[href='" + url + "']").parent().addClass("active");
$subnav.find("a[href='" + url + "']").parent().parent().parent().addClass("start active");

//导航高亮
function highlight_subnav(url){
	$('.page-sidebar-menu').find("a[href='" + url + "']").parent().parent().parent().addClass("start active");
    $('.page-sidebar-menu').find('a[href="'+url+'"]').parent().addClass('active');
}

toastr.options = {
	positionClass: 'toast-top-full-width',
	timeOut: '1500',
	onclick: null
}; 

var options = { 
	resetForm: false, 
	type:'post',
	dataType:  'json' ,
	success: function(data){
		if(data.status) {
			toastr['success'](data.info);
			if(data.url!=='') {
				setTimeout(function() {
					window.location.href = data.url;
				},1500);
			}else{
				setTimeout(function() {
					window.location.reload();
				},1500);
			}
		}else{
			toastr['error'](data.info);
		}
	},
	error: function(data){
		toastr['error'](data.info);
	}
}; 
$('.ajax-post').click(function() { 
	$(this).parents('form').ajaxSubmit(options); 
	$(this).attr('disabled', true);
	setTimeout(function() {
		$('.ajax-post').removeAttr('disabled');
	},1499)
	return false;
}); 

$('.ajax-get').click(function() {
	if(confirm('确定执行该项操作吗？')==false) {
		return false;
	}else{
		$.get(
			$(this).attr('href'),
			function(data) {
				if(data.status) {
					toastr['success'](data.info);
					setTimeout(function() {
						window.location.reload();
					},1500)
				}else{
					toastr['error'](data.info);
				}
			}
		)
	}
	return false;
});


//获取选中的值的id
function get_select_value(id,name){
	var name = name || 'select-name',
	id_list=[];
	$('#'+id+' :checkbox[name="'+name+'"]:checked').each(function(){
		id_list.push($(this).val());
	});
	return id_list;
}

//批量删除
$('#del').click(function(){
	if(confirm('确定删除选中记录？')){
		$.get(
			"{:U('del')}",
			{id: get_select_value('cont')},
			function(data){
				if(data.status){
					toastr['success'](data.info);
					setTimeout(function(){
						location.reload();
					},1500)
				}else{
					toastr['error'](data.info);
				}
			}
			)
	}else{
		return false;
	}
})

//删除单个记录
$('.del').click(function(){
	$.get(
		"{:U('del')}",
		{id : $(this).attr('data-value') },
		function(data){
			if(data.status){
				toastr['success'](data.info);
				setTimeout(function(){
					location.reload();
				},1500)
			}else{
				toastr['error'](data.info);
			}
		}
		)
});

